import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_modular/flutter_modular.dart';
import 'package:thinking/config/_.dart';
import 'package:thinking/entity/_.dart';
import 'package:thinking/service/_.dart';
import 'package:thinking/utils/_.dart';
import 'package:thinking/widget/_.dart';

class MoreRecentVisit extends StatefulWidget{

  @override
  _MoreRecentVisit createState() => _MoreRecentVisit();

}

class _MoreRecentVisit extends State<MoreRecentVisit>{

  List<UserData> userData = [];
  late Future<dynamic> _function;
  late  PageEntity _pageEntity;
  late EasyRefreshController _easyRefreshController;



  future() async {
    await getValue();
  }

  Future<void> getValue() async {

    var res =  (await SearchService.getRecentVisitUserData(params: {
      "Page":_pageEntity.page,
      "count":_pageEntity.count,
      "user_Id": SPreferences().getInt('user_Id')
    }))!;

    if (!_pageEntity.canLoad) {
      BotToast.showText(text: '没有更多用户了哦~');
    } else {
      BotToast.showText(text: '更新了${res.length}条用户~');
    }
    if (res.length < _pageEntity.count && _pageEntity.canLoad) {
      _pageEntity.setPage = _pageEntity.page + 1;
      _pageEntity.setCanLoad = false;
    }
    userData = [...userData,...res];
    setState(() {

    });

  }

  Widget easyRefresh(Widget child) {
    return EasyRefresh(
      child: child,
      header: BezierCircleHeader(backgroundColor: ColorConfig.ThemeColor),
      footer: BezierBounceFooter(backgroundColor: ColorConfig.ThemeColor),
      enableControlFinishRefresh: false,
      enableControlFinishLoad: true,
      controller: _easyRefreshController,
      bottomBouncing: true,
      onLoad: () async {
        if (_pageEntity.canLoad){
          _pageEntity.setPage = _pageEntity.page + 1;
        }

        await getValue();
        _easyRefreshController.finishLoad(success: true);
      },
    );
  }

  Widget title(){
    return Row(
      children: [
        IconButton(onPressed: (){

          Modular.to.pop();

        }, icon: Icon(Icons.arrow_back),color: ColorConfig.WhiteBackColor,iconSize: 20,),
        Text(
          '最近访问',
          style: TextStyle(
              color: ColorConfig.WhiteBackColor,
              fontSize: 16,
              fontWeight: FontWeight.bold
          ),
        )
      ],
    );
  }

  @override
  void initState() {

    super.initState();
    _pageEntity = PageEntity();
    _easyRefreshController = EasyRefreshController();
    _function = future();
  }

  @override
  void dispose() {
    _easyRefreshController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: _function,
      builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
      return snapshot.connectionState == ConnectionState.done?Scaffold(
        appBar: AppBar(
          backgroundColor: ColorConfig.ThemeColor,
          automaticallyImplyLeading: false,
          elevation: 0,
          title: title(),
        ),
        body: easyRefresh(
            Empty(child: ListView(
              shrinkWrap: true,
              physics: BouncingScrollPhysics(),
              children:userData.map((e) => GestureDetector(
                onTap: (){
                  // Modular.
                },
                child: GestureDetector(
                  onTap: (){
                    Modular.to.pushNamed('/personal/${e.userId}');
                  },
                  child: UserItemWidget(userData: e,showButton: e.userId != SPreferences().getInt('user_Id'),),
                ),
              )).toList(),
            ),isShow: userData.length == 0,)
        ),
      ):Scaffold(
        body: Loading(),
      );
    },);
  }

}